﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using cancerportal.Entitys;
namespace cancerportal.Procedure

{
    public class DiseaseProcedure 
    {
        // get all disease of a list
        public List<disease> getDiseaseWithWeight(string listS, float range)
        {
            System.Diagnostics.Debug.WriteLine(listS);
            int diseaseid = 0;
            List<disease> listDisease = new List<disease>();
            SqlConnection conn = null;
            SqlDataReader buffer = null;
            try
            {
                string connection = cancerportal.Properties.Settings.Default.StringConnection;
                conn = new SqlConnection(connection);
                SqlCommand cmd = new SqlCommand("SelectByIdList", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter id = new SqlParameter("@listSymptom",SqlDbType.Xml);
                id.Value = listS;
                SqlParameter ran = new SqlParameter("@range", SqlDbType.Float);
                ran.Value = range;
                cmd.Parameters.Add(id);
                cmd.Parameters.Add(ran);
                conn.Open();
                buffer = cmd.ExecuteReader();
                while (buffer.Read())
                {
                    diseaseid = Convert.ToInt32(buffer["id"]);
                    //diseaseid, current weight, list of relation symptom to suggest
                    disease objtmp = new disease(diseaseid, Convert.ToSingle(buffer["weight"]), this.getRelationSymptom(diseaseid, listS, range));
                    listDisease.Add(objtmp);
                }
            }
            catch (SqlException x) {
                System.Diagnostics.Debug.WriteLine(x.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (buffer != null)
                {
                    buffer.Close();
                }
            }
            return listDisease;
        }

        //get all relation symptom of list initial symptom
        public List<Symptom> getRelationSymptom(int id, string listS, float range)
        {
            List<Symptom> relSymptom = new List<Symptom>();
            SqlConnection conn = null;
            SqlDataReader buffer = null;
            try
            {
                string connection = cancerportal.Properties.Settings.Default.StringConnection;
                conn = new SqlConnection(connection);
                SqlCommand cmd = new SqlCommand("RemainList", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter did = new SqlParameter("@disease", SqlDbType.Int);
                did.Value = id;
                SqlParameter list = new SqlParameter("@listSymptom", SqlDbType.Xml);
                list.Value = listS;
                SqlParameter ran = new SqlParameter("@range", SqlDbType.Float);
                ran.Value = range;
                cmd.Parameters.Add(did);
                cmd.Parameters.Add(list);
                cmd.Parameters.Add(ran);
                conn.Open();
                buffer = cmd.ExecuteReader();
                while (buffer.Read())
                {
                    //symptom + 'weight in disease' + 'check user have this'
                    Symptom obj = new Symptom(Convert.ToInt32(buffer["symptom"]), Convert.ToSingle(buffer["weight"]), false);
                    relSymptom.Add(obj);
                }
            }
            catch (SqlException x)
            {
                System.Diagnostics.Debug.WriteLine(x.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (buffer != null)
                {
                    buffer.Close();
                }
            }
            return relSymptom;
        }

        //get all symptom is unique
        public List<symptomEntity> getUniqueSymptom(string listD, string listS)
        {
            List<symptomEntity> allSymptom = new List<symptomEntity>();
            SqlConnection conn = null;
            SqlDataReader buffer = null;
            try
            {
                string connection = cancerportal.Properties.Settings.Default.StringConnection;
                conn = new SqlConnection(connection);
                SqlCommand cmd = new SqlCommand("AllSymptom", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter listDisease = new SqlParameter("@listDisease", SqlDbType.Xml);
                listDisease.Value = listD;
                SqlParameter listSymptom = new SqlParameter("@listSymptom", SqlDbType.Xml);
                listSymptom.Value = listS;
                cmd.Parameters.Add(listDisease);
                cmd.Parameters.Add(listSymptom);
                conn.Open();
                buffer = cmd.ExecuteReader();
                while (buffer.Read())
                {
                    symptomEntity obj = new symptomEntity();
                    obj.symptom_id = Convert.ToInt32(buffer["id"]);
                    obj.symptom_name = Convert.ToString(buffer["name"]);
                    allSymptom.Add(obj);
                }
            }
            catch (SqlException x)
            {
                System.Diagnostics.Debug.WriteLine(x.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (buffer != null)
                {
                    buffer.Close();
                }
            }
            return allSymptom;
        }
    }
}